using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using Elev.Core.DAL;
using Elev.Core.Utility;

namespace Elev.Core
{
    public class Period : IDimension
    {
        int id;
        string name;
        bool showAvgByType;
        bool showGlobalAvg;
        int displayOrder;

        public int ID { get { return id; } }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public bool ShowAvgByType
        {
            get { return showAvgByType; }
            set { showAvgByType = value; }
        }

        public bool ShowGlobalAvg
        {
            get { return showGlobalAvg; }
            set { showGlobalAvg = value; }
        }

        public int DisplayOrder
        {
            get { return displayOrder; }
            set { displayOrder = value; }
        }

        public Period(DataRow row)
        {
            parse(row);
        }

        #region Static members

        /// <summary>
        /// Swaps order between two periods.
        /// p1.DisplayOrder becomes p2.DisplayOrder
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        static public void SwapOrder(Period p1, Period p2)
        {
            int p1Order = p1.DisplayOrder;
            int p2Order = p2.DisplayOrder;
            p1.DisplayOrder = 100000;
            p1.Save();
            p2.DisplayOrder = p1Order;
            p2.Save();
            p1.DisplayOrder = p2Order;
            p1.Save();
        }

        #endregion

        public Period() : this(-1, "", true, true, -1) { }

        public Period(int ID, string Name, bool ShowAvgByType, bool ShowGlobalAvg, int DisplayOrder)
        {
            id = ID;
            name = Name;
            showAvgByType = ShowAvgByType;
            showGlobalAvg = ShowGlobalAvg;
            displayOrder = DisplayOrder;
        }

        void parse(DataRow dr)
        {
            id = Convert.ToInt32(dr["ID"]);
            name = (string)dr["Name"];
            showAvgByType = (Convert.ToInt32(dr["ShowAvgByType"]) == 1);
            showGlobalAvg = (Convert.ToInt32(dr["ShowGlobalAvg"]) == 1);
            displayOrder = Convert.ToInt32(dr["DisplayOrder"]);
        }

        #region IDimension Members

        public int Save()
        {
            PeriodInternal periodInternal = new PeriodInternal();
            if (id == -1)
            {
                // New creation ...
                id = periodInternal.Add(name, showAvgByType, showGlobalAvg, displayOrder);
            }
            else
            {
                periodInternal.Update(id, name, showAvgByType, showGlobalAvg, displayOrder);
            }
            return id;
        }

        public void Delete()
        {
            PeriodInternal periodInternal = new PeriodInternal();
            periodInternal.Delete(id);
        }

        #endregion

        public override string ToString()
        {
            return name;
        }
    }
}
